package com.finace.money.net.socket;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.finace.money.common.Constant;
import com.finace.money.common.util.ACache;
import com.finace.money.common.util.JsonUtils;
import com.finace.money.common.util.UIUtil;
import com.finace.money.data.bean.BankContractBean;
import com.finace.money.data.bean.CashOperationBean;
import com.finace.money.data.bean.LoginBean;
import com.finace.money.data.bean.MessageEndBean;
import com.finace.money.data.bean.OrderErrorBean;
import com.finace.money.data.bean.PasswordBean;
import com.finace.money.data.bean.TickBean;

/**
 * Created by zhangpeng on 2017/12/8.
 */

public class PushReceiver extends BroadcastReceiver {
    public static final String ACTION = "com.java.finace.action";
    public static final String ACTION_HISTORY = "com.java.finace.action.history";
    public static final String ACTION_HISTORY_UPDATE = "com.java.finace.action.history.update";
    public static final String ACTION_TICK = "com.java.finace.action.tick";
    public static final String ACTION_LOGIN = "com.java.finace.action.login";
    public static final String ACTION_QRY = "com.java.finace.action.qry";
    public static final String ACTION_QRY_ERROR = "com.java.finace.action.qry.error";
    public static final String ACTION_ACCOUNT_INFO = "com.java.finace.action.account";
    public static final String ACTION_CHANGE_PASSWORD = "ChangePasswordResponse";
    public static final String ACTION_LIST = "list";
    public static final String ACTION_BANK_CHANGE = "bank_change";
    public static final String ACTION_INFO_UPDATE = "info_update";
    public static final String ACTION_MONEY_RECORD = "money_record";
    public static final String ACTION_CASH_OPERATION = "cash_operation";
    public static final String CONN_SUCCESS = "conn_success";
    public static final String DATA = "data";
    public static final String FINISH = "finish";
    public static final String UPDATE = "update";
    public static final String MESSAGE = "message";
    public static final String QRY_POSITION_END = "qry_position";
    public static final String QRY_TRADE_END = "qry_trade";
    public static final String QRY_ORDER_END = "qry_order";
    public static final String QRY_SETTLEINFO_END = "qry_settleinfo";


    @Override
    public void onReceive(final Context context, final Intent intent) {

        final String msg = intent.getStringExtra(DATA);
        new Thread() {
            @Override
            public void run() {
                super.run();

                if (msg.contains(CONN_SUCCESS)) {
                    Intent conn = new Intent();
                    conn.setAction(CONN_SUCCESS);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(conn);
                } else if (msg.contains("QUERY_END")) {
                    MessageEndBean messageEndBean = JsonUtils.fromJsonToBaseConn(msg, MessageEndBean.class);
                    if (messageEndBean.getM_CorrespondingRequestType() == 11) {
                        Intent list = new Intent();
                        list.setAction(ACTION_LIST);
                        list.putExtra(FINISH, FINISH);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(list);
                    } else if (messageEndBean.getM_CorrespondingRequestType() == 20) {
                        Intent his = new Intent();
                        his.setAction(ACTION_HISTORY);
                        his.putExtra(FINISH, FINISH);
                        LocalBroadcastManager.getInstance(context).sendBroadcast(his);
                    }
                } else if (msg.contains("CandleNotify")) {
                    Intent intentHisUpdate = new Intent();
                    intentHisUpdate.setAction(ACTION_HISTORY_UPDATE);
                    intentHisUpdate.putExtra(UPDATE, UPDATE);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentHisUpdate);
                } else if (msg.contains("Tick") || msg.contains("TickSnapshot")) {//待测试  && msg.contains("SR805")
                    TickBean tickBean = JsonUtils.fromJsonToBaseConn(msg, TickBean.class);
                    Intent intentTick = new Intent();
                    intentTick.setAction(ACTION_TICK);
                    intentTick.putExtra(DATA, tickBean);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentTick);
                } else if (msg.contains("LoginResponse")) {
                    LoginBean loginBean = JsonUtils.fromJsonToBaseConn(msg, LoginBean.class);
                    ACache.get(UIUtil.getContext()).put(Constant.login, loginBean.getLogin_id());
                    ACache.get(UIUtil.getContext()).put(Constant.account, loginBean.getAccount());
                    Intent intentTick = new Intent();
                    intentTick.setAction(ACTION_LOGIN);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentTick);
                } /*else if (msg.contains(Constant.ACCOUNT_INFO)) {
                    Intent intentQry = new Intent();
                    intentQry.setAction(ACTION_ACCOUNT_INFO);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                } */ else if (msg.contains("ChangeInvestorResponse")) {
                    BankContractBean changeSucess = JsonUtils.fromJsonToBaseConn(msg, BankContractBean.class);
                    Intent intentChange = new Intent();
                    intentChange.setAction(ACTION_BANK_CHANGE);
                    intentChange.putExtra(ACTION_BANK_CHANGE, changeSucess);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentChange);
                } else if (msg.contains("QryInvestorResponse")) {
                    Intent intentUpdateInfo = new Intent();
                    intentUpdateInfo.setAction(ACTION_INFO_UPDATE);
                    intentUpdateInfo.putExtra(ACTION_INFO_UPDATE,ACTION_INFO_UPDATE);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentUpdateInfo);
                } else if (msg.contains("QryTransferSerialResponse")) {
                    Intent intentMoneyRecord = new Intent();
                    intentMoneyRecord.setAction(ACTION_MONEY_RECORD);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentMoneyRecord);
                } else if (msg.contains(QRY_POSITION_END)) {
                    Intent intentQry = new Intent();
                    intentQry.setAction(ACTION_QRY);
                    intentQry.putExtra(QRY_POSITION_END, QRY_POSITION_END);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                } else if (msg.contains(QRY_TRADE_END)) {
                    Intent intentQry = new Intent();
                    intentQry.setAction(ACTION_QRY);
                    intentQry.putExtra(QRY_TRADE_END, QRY_TRADE_END);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                } else if (msg.contains(QRY_ORDER_END)) {
                    Intent intentQry = new Intent();
                    intentQry.setAction(ACTION_QRY);
                    intentQry.putExtra(QRY_ORDER_END, QRY_ORDER_END);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                } else if (msg.contains(QRY_SETTLEINFO_END)) {
                    Intent intentQry = new Intent();
                    intentQry.setAction(QRY_SETTLEINFO_END);
                    intentQry.putExtra(QRY_SETTLEINFO_END, QRY_SETTLEINFO_END);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                } else if (msg.contains("OrderErrorNotify")) {
                    OrderErrorBean errorBean = JsonUtils.fromJsonToBaseConn(msg, OrderErrorBean.class);
                    Intent intentError = new Intent();
                    intentError.setAction(ACTION_QRY_ERROR);
                    intentError.putExtra(ACTION_QRY_ERROR, errorBean.getRsp_info().getError_message());
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentError);
                } else if (msg.contains(ACTION_CHANGE_PASSWORD)) {
                    PasswordBean passwordBean = JsonUtils.fromJsonToBaseConn(msg, PasswordBean.class);
                    Intent intentQry = new Intent();
                    intentQry.setAction(ACTION_CHANGE_PASSWORD);
                    intentQry.putExtra(ACTION_CHANGE_PASSWORD, passwordBean);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                } else if (msg.contains("CashOperationResponse")) {
                    CashOperationBean cashOperationBean = JsonUtils.fromJsonToBaseConn(msg, CashOperationBean.class);
                    Intent intentQry = new Intent();
                    intentQry.setAction(ACTION_CASH_OPERATION);
                    intentQry.putExtra(ACTION_CASH_OPERATION, cashOperationBean);
                    LocalBroadcastManager.getInstance(context).sendBroadcast(intentQry);
                }

            }
        }.start();

    }
}
